// var url = "http://h5.tsshzl.ubo.net:8848" + window.location.pathname
// let Test = "http://api.tsshzl.ubo.net"
// let formal = "http://api.tsshzl.ubo.net"
let formal = "http://api.tangshan.youbo.me" //线上地址
// let formal = 'http://api.tsshzl.11.youbo.me' //测试地址
// let formal = 'http://api.tsshzl.ubo.net' //后端本地地址
let getToken = localStorage.getItem("token")

let userInfo = JSON.parse(localStorage.getItem("userInfo"))
let headers = {}
var HttpRequest = function (options) {
  var defaults = {
    type: 'get',
    headers: {},
    data: {},
    dataType: 'json',
    async: true,
    cache: false,
    beforeSend: null,
    success: null,
    complete: null,
    error: null
  };
  var o = $.extend({}, defaults, options);
  $.ajax({
    url: o.url,
    type: o.type,
    headers: {
      'Content-Type': o.contentType,
      'Authorization': o.token
    },
    data: o.data,
    dataType: o.dataType,
    async: o.async,
    beforeSend: function (res) {
      o.beforeSend && o.beforeSend();
    },
    success: function (res) {
      o.success && o.success(res);
    },
    complete: function (res) {
      o.complete && o.complete();
      if (res.status == 403) {
        if (document.getElementsByClassName('el-message').length === 0) {
          vue.$message.closeAll()
          vue.$message({
            message: res.responseJSON.message,
            type: 'error',
            duration: 3000
          });
          vue.$data.loading = false
        }
      }
      if (res.status == 401) {
        localStorage.removeItem("token")
        window.location.href = "login.html"
      }
      if (res.status == 500 || res.status == 404) {
        if (o.url.indexOf('/auth/wechat-login') != -1) {
          window.location.href = "../login.html"

        } else {
          vue.$message.closeAll()
          vue.$message({
            message: '服务器错误',
            type: 'error',
            duration: 3000
          });
        }

      }
    },
    error: function (res) {
      o.error && o.error(res);
    }
  });
};

var loginHttp = function (options) {
  // 登入页无需携带token
  // 后台如果要求 Content-Type 
  if (options.type == 'post') {
    options.contentType = 'application/x-www-form-urlencoded';
  }
  HttpRequest(options);
}
var ajax = function (options) {
  if (options.type == 'post') {
    options.contentType = 'application/x-www-form-urlencoded';
  }
  // 每次请求携带token

  if (localStorage.getItem("token")) {
    let nowStr = Date.parse(new Date()) / 1000
    let jwtData = jwt_decode(localStorage.getItem("token"))
    if (nowStr > jwtData.exp) {
      localStorage.removeItem('token')
      window.location.href = "../login.html"
    }
  }
  options.token = 'Bearer ' + localStorage.getItem('token');
  HttpRequest(options);
}


function showErrorTip(tip) {
  $('#errorTip').text(tip);
  $('#errorTip').fadeIn();
  setTimeout(function () {
    $('#errorTip').fadeOut();
  }, 2000);
}